package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    //新增班级
    @Override
    public void add(Clazz clazz) {

        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    //分页查询数据
    @Override
    public PageResult page(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());

        List<Clazz> list = clazzMapper.list(clazzQueryParam);
        Page<Clazz> p = (Page<Clazz>) list;
        return new PageResult(p.getTotal(),list);
    }



    //删除数据
    @Override
    public Integer delete(Integer id) {
        Integer i = clazzMapper.delete(id);
        return i;
    }

    //修改
    //第一步：回显
    @Override
    public Clazz select(Integer id) {

        return clazzMapper.getById(id);
    }
    //第二步：修改数据
    @Override
    public Integer update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        return clazzMapper.update(clazz);
    }
    //查询全部
    @Override
    public List<Clazz> findAllto() {
        return clazzMapper.findAllto();
    }
}
